/**
 * Created by zenoniaonline on 14. 4. 8.
 */


var jade = require('jade');
var util = require('util');

exports.Test_showTables = function (req, res) {
    var DBPool = req.app.get('Test_Game_Master');

    DBPool.getConnection(function (err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("show Tables;");

        conn.query(strQuery, function (err, rows, fields) {
            conn.release();
            if (err) {
                console.log(err);
            } else {
                res.render('jjaehuny/Test/showTables', { type: "TEST SERVER", server: "TEST SERVER", rows: rows, fields: fields });
            }
        })
    })
};

exports.Test_showTable = function(req, res){

    var tableName = req.param('tableName');

    var DBPool = req.app.get('Test_Game_Master');
    DBPool.getConnection(function(err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("select * from %s;", tableName);
        console.log("query : " + strQuery);

        conn.query(strQuery, function (err, rows, fields) {
            if (err) {
                console.log(err);
            } else {
                console.log(rows);
                res.render('jjaehuny/layout', { type: "TEST SERVER", rows: rows, fields: fields });
            }
            conn.release();
        })
    })
};

exports.Test_showCurrentUser = function(req, res){

    var DBPool = req.app.get('Test_Log_Master');

    DBPool.getConnection(function(err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("select * from serveraddr_ccu;");

        conn.query(strQuery, function(err, rows, fields) {
            conn.release();
            if (err) {
                console.log(err);
            } else {
                console.log(rows);
                res.render('jjaehuny/layout', { type:"TEST SERVER",server:"TEST SERVER", rows:rows, fields:fields });
            }
        })
    })
};

exports.Test_find_info_login_socialid = function(req, res) {

    var socialid = req.param('socialid');
    console.log(socialid);

    var DBPool = req.app.get('Test_Game_Master');

    DBPool.getConnection(function (err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("select * from info_login where socialid = '%s';", socialid);

        conn.query(strQuery, function (err, rows, fields) {
            conn.release();
            if (err) {
                console.log(err);
            } else {
                res.render('jjaehuny/layout', { type: "TEST SERVER", rows: rows, fields: fields });
            }
        })
    })
};

exports.Test_find_info_login_seq = function(req, res) {

    var seq = req.param('seq');
    console.log(seq);

    var DBPool = req.app.get('Test_Game_Master');

    DBPool.getConnection(function (err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("select * from info_login where seq = %s;", seq);

        conn.query(strQuery, function (err, rows, fields) {
            conn.release();
            if (err) {
                console.log(err);
            } else {
                res.render('jjaehuny/layout', { type: "TEST SERVER", rows: rows, fields: fields });
            }
        })
    })
};

exports.Test_find_info_user_socialid = function(req, res){

    var socialid = req.param('socialid');

    var DBPool = req.app.get('Test_Game_Master');

    DBPool.getConnection(function(err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("select * from info_user as a inner join info_login as b on a.user_seq = b.seq where b.socialid = '%s';", socialid);

        conn.query(strQuery, function(err, rows, fields) {
            conn.release();
            if (err) {
                console.log(err);
            } else {
                res.render('jjaehuny/layout', { type:"TEST SERVER",rows: rows, fields: fields });
            }
        })
    })
};

exports.Test_find_info_user_seq = function(req, res){

    var seq = req.param('seq');

    var DBPool = req.app.get('Test_Game_Master');

    DBPool.getConnection(function(err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("select * from info_user where user_seq = '%s';", seq);

        conn.query(strQuery, function(err, rows, fields) {
            conn.release();
            if (err) {
                console.log(err);
            } else {
                res.render('jjaehuny/layout', { type:"TEST SERVER",rows: rows, fields: fields });
            }
        })
    })

};

exports.Test_find_info_user_nickname = function(req, res){

    var nickname = req.param('nickname');

    var DBPool = req.app.get('Test_Game_Master');

    DBPool.getConnection(function(err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("select * from info_user where nickname = '%s';", nickname);

        conn.query(strQuery, function(err, rows, fields) {
            conn.release();
            if (err) {
                console.log(err);
            } else {
                res.render('jjaehuny/layout', { type:"TEST SERVER",rows: rows, fields: fields });
            }
        })
    })
};

exports.Test_find_info_character_user_seq = function(req, res){

    var user_seq = req.param('user_seq');

    var DBPool = req.app.get('Test_Game_Master');

    DBPool.getConnection(function(err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("select * from info_character where user_seq = '%s';", user_seq);

        conn.query(strQuery, function(err, rows, fields) {
            conn.release();
            if (err) {
                console.log(err);
            } else {
                res.render('jjaehuny/layout', { type:"TEST SERVER",rows: rows, fields: fields });
            }
        })
    })
};

exports.Test_find_info_character_seq = function(req, res){

    var seq = req.param('seq');

    var DBPool = req.app.get('Test_Game_Master');

    DBPool.getConnection(function(err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("select * from info_character where seq = '%s';", seq);

        conn.query(strQuery, function(err, rows, fields) {
            conn.release();
            if (err) {
                console.log(err);
            } else {
                res.render('jjaehuny/layout', { type:"TEST SERVER",rows: rows, fields: fields });
            }
        })
    })
};

exports.Test_update_env_block = function(req, res){

    var block = req.param('block');

    var DBPool = req.app.get('Test_Game_Master');

    DBPool.getConnection(function(err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("update env_config set isBlock = %s;", block);

        conn.query(strQuery, function(err, rows, fields) {
            conn.release();
            if (err) {
                console.log(err);
                res.render('jjaehuny/Result', { title: 'Express', result:"fail" });
            } else {
                var strQuery = util.format("select seq, service_url, isBlock from env_config");
                conn.query(strQuery, function(err, rows, fields) {
                    conn.release();
                    if (err) {
                        console.log(err);
                    } else {
                        res.render('jjaehuny/layout', { type:"TEST SERVER",rows: rows, fields: fields });
                    }
                })
            }
        })
    })
};


exports.Test_update_contents_open = function(req, res){
    var content = req.param('content');
    var onoff = req.param('onoff');

    var DBPool = req.app.get('Test_Game_Master');

    DBPool.getConnection(function(err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("update resource_contents_open set isOpen = %s where contents = %s;", onoff, content );

        conn.query(strQuery, function(err, rows, fields) {
            conn.release();
            if (err){
                console.log(err);
                res.render('jjaehuny/Result', { title: 'Express', result:"fail" });
            } else {
                var strQuery = util.format("select * from resource_contents_open");
                conn.query(strQuery, function(err, rows, fields) {
                    conn.release();
                    if (err) {
                        console.log(err);
                        res.render('jjaehuny/Result', { title: 'Express', result:"fail" });
                    } else {
                        res.render('jjaehuny/layout', { type:"TEST SERVER",rows: rows, fields: fields });
                    }
                })
            }
        })
    })
};

exports.Test_update_vip = function(req, res) {

    var seq = req.param('seq');
    var point = req.param('point');

    var DBPool = req.app.get('Test_Game_Master');

    DBPool.getConnection(function(err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("update info_user set reserve3 = unix_timestamp(now()), reserve4 = %s where user_seq = %s;", point, seq);

        conn.query(strQuery, function(err, rows, fields) {
            conn.release();
            if (err){
                console.log(err);
                res.render('jjaehuny/Result', { title: 'Express', result:"fail" });
            } else {
                var strQuery = util.format("select * from info_user where user_seq = %s;", seq);
                conn.query(strQuery, function(err, rows, fields) {
                    conn.release();
                    if (err) {
                        console.log(err);
                        res.render('jjaehuny/Result', { title: 'Express', result:"fail" });
                    } else {
                        res.render('jjaehuny/layout', { type:"TEST SERVER",rows: rows, fields: fields });
                    }
                })
            }
        })
    })
};


